/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *EhrCollegeController
 * 学校档案
 *
 * 修改纪录
 * 2020-09-03 版本：1.0 wangxinhua 创建。
 * @version 版本：1.0
 * @author 作者：wangxinhua
 * 创建日期：2020-09-03
 */

package com.topscomm.base.controller;

import com.topscomm.base.controller.auto.BaseCollegeControllerAuto;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @author: wangxinhua
 * @date: 2020-09-03
 * @description:学校档案Controller
 * @modify:
 */
@RestController
@RequestMapping("base/college")
public class BaseCollegeController extends BaseCollegeControllerAuto {
	/**
	 * @author: wangxinhua
	 * @date: 2020-09-03
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
	    this.getUserBean();
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
        if (paraMap.containsKey("qry_name")) {
            String value = paraMap.get("qry_name").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and name like '%{0}%' ", value);
        }

        if (paraMap.containsKey("qry_code")) {
            String value = paraMap.get("qry_code").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and code like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_regional")) {
            String value = paraMap.get("qry_regional").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and regional like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_collegelevel")) {
            String value = paraMap.get("qry_collegelevel").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and collegelevel like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_contactinformation")) {
            String value = paraMap.get("qry_contactinformation").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and contactinformation like '%{0}%' ", value);
        }
        if (paraMap.containsKey("qry_state")) {
            String value = paraMap.get("qry_state").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and state = '{0}' ", value);
        }
        if (paraMap.containsKey("qry_begindocdate")) {
            String value = paraMap.get("qry_begindocdate").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
        }
        if (paraMap.containsKey("qry_enddocdate")) {
            String value = paraMap.get("qry_enddocdate").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
        }
        if (paraMap.containsKey("qry_docno")) {
            String value = paraMap.get("qry_docno").toString();
            if (!StringUtil.isEmpty(value))
                sql.appendSingle(" and docno = '{0}' ", value);
        }
        if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
        {
            String dataPession = service.getDataPermission(TapSystemConst.TapOperationTypeEnum.Select, "", "EhrCollege",
                    this.getUserBean());
            if (StringUtil.isEmpty(dataPession)) {
                sql.appendSingle("and enabled = 1");
            } else// 使用数据权限
            {
                sql.appendSingle(" and {0} ", dataPession);
            }
        }
		return sql.toString();
	}

	/**
	 * @author: wangxinhua
	 * @date: 2020-09-03
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: wangxinhua
	 * @date: 2020-09-03
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}于{1}提交学校档案申请,单据编号{2}! ",  dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
	}
}

